<template>
  <div class="login">
    <div class="login-form">
      <h3 class="welcome-message">欢迎登录小区物业管理系统</h3>
      <el-form
        ref="form"
        :model="FormData"
        :rules="rules"
        label-width="80px"
        style="margin-top: 60px"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="FormData.username"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="FormData.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-input
            v-model="captcha"
            placeholder="请输入验证码"
            style="width: 60%; float: left"
          ></el-input>
          <img
            :src="imgPath"
            alt="验证码"
            @click="getCodeImg"
            style="width: 35%; cursor: pointer"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="danger" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      captcha: "",
      FormData: {
        username: "",
        password: "",
      },
      imgPath: "", // 验证码图片路径
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async submitForm() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return this.$message.error("请填写正确表单信息");

        const response = await axios.post(
          "users/login?admin=" +
            this.FormData.username +
            "&password=" +
            this.FormData.password,
          {
            username: this.FormData.username,
            password: this.FormData.password,
          }
        );
        // console.log(response);
        if (response.status === 200) {
          localStorage.setItem(
            "username",
            JSON.stringify({ username: this.FormData.username })
          );
          sessionStorage.setItem("token", response.data.jwt_token);
          this.$router.push("/home/index");
          this.$message.success("登录成功");
        } else {
          console.log("请求失败，状态码:", response.status);
        }
      } catch (error) {
        this.$message.error("登陆错误 请重试！");
      }
    },
    handleReset() {
      this.$refs.form.resetFields();
    },
    getCodeImg() {
      // 获取验证码图片的逻辑
      this.imgPath = "path/to/captcha/image"; // 替换为实际的验证码图片路径
    },
  },
  created() {
    this.getCodeImg(); // 初始化时获取验证码图片
  },
};
</script>

<style lang="scss" scoped>
.el-button {
  width: 40%;
  height: 35px;
  font-size: 14px;
}

.login {
  background-image: url("../assets/img/123.jpg");
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.welcome-message {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  color: #333;
}

.login-form {
  position: relative;
  border: 1px solid #dcdfe6;
  width: 470px;
  height: 350px;
  background-color: hsla(0, 0%, 100%, 0.85);
  padding: 35px 85px 15px 35px;
  border-radius: 5px;
  box-shadow: 0 0 25px #909399;
}
</style>
